import * as React from "react";
import Modal from "antd/es/modal/Modal";
import {Button, Card, Col, Form, Input, InputNumber, notification, Row} from "antd";
import {CarryOutOutlined, UserOutlined} from "@ant-design/icons";
import {createKey} from "../../requests/KeyManageRequest";


class CreateVerifyKeyModal extends React.Component{

    constructor(props) {
        super(props);
        this.state = {
            isModalVisible: false,
            formRef: React.createRef(),
        };
        this.props.onRef(this)
    }

    showModal = () => {
        this.setState({
            isModalVisible: true
        })
    };

    handleCancel = () => {
        this.setState({
            isModalVisible: false
        })
    };

    handleOk = () => {
        this.state.formRef.current.validateFields()
            .then(values => {
                console.log("Begin to create verify key with params:" + JSON.stringify(values))
                createKey(values).then(edpKey => {
                    notification.info({message: "创建激活码成功", description: "创建激活码" + edpKey.result.keyName + "成功"});
                })
                this.setState({
                    isModalVisible: false
                })
            })
            .catch(info => {
                console.log('Validate message:', info);
            });
    };

    render = () => {
        return (
            <Modal title="创建激活码" visible={this.state.isModalVisible} onOk={this.handleOk} onCancel={this.handleCancel}>
                <Form name="basic" ref={this.state.formRef}>
                    <Form.Item
                        label="激活码可用总次数" name="totalTimes"
                        rules={[{required: true, message: '请输入激活码的可预测次数！'}]}>
                        <InputNumber prefix={<CarryOutOutlined/>}  />
                    </Form.Item>

                    <Form.Item
                        label="激活码有效总天数" name="effectiveDurationInDay"
                        rules={[{required: true, message: '请输入激活码的有效时长！'}]}>
                        <InputNumber/>
                    </Form.Item>

                    <Form.Item
                        label="激活码的发放对象" name="username"
                        rules={[{required: true, message: '请输入激活码的发放对象'}]}>
                        <Input/>
                    </Form.Item>

                    <Form.Item
                        label="激活码的备注信息" name="remark">
                        <Input/>
                    </Form.Item>
                </Form>
            </Modal>
        );
    }
}

export default CreateVerifyKeyModal